// JavaScript Document
Screen = {
	/*手机浏览器的大小*/
	size:function(){
		return [parseInt($("#pokeyBox").css("width")),$("#pokeyBox").height()]
		}
	}
var Video;	
require(["js/mVideo.js"], function(video){  
	Video = video;
});		
/*
MyPoker类
	公共属性
	---------------------------------------------------------
	pokeyBox -- 放牌的盒子
	putPokeyBox -- 出牌的盒子
	pokeyButton -- 按钮的盒子
	myMsg -- 消息的按钮
	myPokeyName  -- 名字的盒子
	myPokeyHead -- 头像的盒子
	threePoker -- 三张牌的盒子
	---------------------------------------------------------
	公共方法：
	get ---  得到牌
	put --- 出牌
	_button --- 显示按钮管理 
			1、reday  【准备】
			2、bid 【不叫，1分，2分，3分】
			3、pull 【拉、不拉】
			4、put 【出、不出】
	-----------------------------------------
	私有方法
	_createPokey 创建手牌

*/

/*牌 cardSpades黑桃 cardHearts红桃 cardPlum梅花  cardSquare方片*/
	var pokeyArray = ["cardSpades","cardHearts","cardPlum","cardSquare"];
	var pokeyArrayNum = ["3","4","5","6","7","8","9","10","J","Q","K","A","2"];
	
	function MyPoker(name){
		this.name =name;
		this.noPutNums=0;
		this.pokeyBox = $("#pokeyBox");
		this.putPokeyBox = $("#putPokeyBox");
		this.pokeyButton = $("#myButton");
	}
	
	
	MyPoker.prototype = {
		
		/*进入房间
			绑定牌的事件
			设置工具栏
		*/
		goin:function(){
			toolBar.init();
			//toolBar.hideBack();
			room._dieEvent();
			this._bindEvent();			
			console.log("Q-->你进入房间");
			
		},
		
		/*得到牌
		
		 * 创建手牌 this._createPokey(arr);
		 * 显示展开牌的动画 this._animationShowPoker(arr);
		 * 创建属性 this._proto();
		*/	
		get:function(arr){
				console.log("Q-->你发了17张牌："+arr);
				Video.get();
				this._createPokey(arr);
				this._setPokeyMargin();
				this._proto();
				var _t = this;
				setTimeout(function(){
					_t._startSetPost();
					_t._animationShowPoker(arr);
				},0)
			},
		
		/*重连时直接获取牌
		 
		 * 创建手牌 this._createPokey(arr);
		 * 显示牌【无需动画显示】
		 * */
		reGet:function(arr,isShowBgPokey){
			console.log("Q-->你重连时的牌："+arr);
			Video.get();
			this._createPokey(arr);
			this._setPokeyMargin();
			this._proto();
			var _t = this;
			setTimeout(function(){
				if(!isShowBgPokey){
					_t._startSetPost(true);
				}
				_t.pokeyBox.find(".card").css({
					"opacity":100										   
				});
			},0)
			
			
			
			
			
		},
			
		/*把三张牌插入手牌中,并显示插入效果
		 * 创建三张底牌this._createBottomPokey(arr);
		 * 设置牌的间距this._setPokeyMargin();
		 * 设置三张底牌的位置 this._setBottomPokerPost(arr);
		 * 顶部得到三张牌toolBar.getBottomPokey(arr);
		*/
		
		getBottomPoker:function(arr){
			console.log("Q-->你发了3张底牌："+arr);
			this._createBottomPokey(arr);
			this._setPokeyMargin();
			this._setBottomPokerPost(arr);
			toolBar.getBottomPokey(arr);
			},
			
			
			
		/*点击出牌按钮
		 * 获取点击时得到的牌 this._getPut();
		 * 判断  如果没有选择牌 
		 * 规则验证
		 * 删除已选择的牌 this._removePut();
		 * 清空按钮this.button().clear();
		 * 设置牌的间距this._setPokeyMargin();
		 * 隐藏我的牌信息 player.hideMsg("my");
		 * 显示出了牌在桌面上 player.put("my",putArr);
		*/
		put:function(isMustPut){
				var putArr = this._getPut();
				console.log("Q-->-----请选择牌" + putArr.length + " ----"+putArr);
				/*没有选择牌，就点出牌*/
				if(putArr.length === 0){
					//player.msg("my","text-selectPokey");
					player.systemMsg("text-selectPokey");
					return;
					}
					
				/*
				是不是我先出牌
				*/
				
				if(isMustPut){
					var type = Rule.getSType(putArr);
						console.log("Q-->我先出--是不是符合出牌规则:"+type);
					if(type == -1){
						//player.msg("my","text-noAccord");
						player.systemMsg("text-noAccord");
						return;
						}
				}else{
					var prevPokey = getPerOutCards();
					console.log("Q-->是不是符合出牌规则:"+Rule.canOut(prevPokey,putArr));
					
					if(!Rule.canOut(prevPokey,putArr)){
						//player.msg("my","text-noAccord");
						player.systemMsg("text-noAccord");
						return;
					}	
				}
				
			
				this._removePut();
				this.button().clear();
				this._setPokeyMargin();
				player.hideMsg("my");
				//player.put("my",putArr);
				/*判断炸弹*/
				
				gameOnOutCard(putArr);
				
				
			
			},
			
			
			
			
		/*按钮管理和进程管理
		*/	
		button:function(){
			var _t = this;
			
			return{
				
				
			
				/*准备*/
				ready:function(){
					//var b = "<input name='' type='button' value='准 备'  class='pokeyButton button-blue ' id='button-ready'/>";
					var buttonArray = ["<input name='' type='button' value='返回大厅'  class='pokeyButton button-blue' id='button-return'/>",
					                   "<input name='' type='button' value='准 备'  class='pokeyButton button-blue' id='button-ready'/>"
									  ];
					var b =  buttonArray[1];	
					_t.pokeyButton.html(b);
					
					var readyButton = $("#button-ready");
					var gameModeType = dataStorage.get(OtherThing.gameModeType);
					 readyButton.click(function(){
							Video.ready();
				    		gameOnReady();
							console.log("Q-->你点击“准备”按钮");
							
							_t.button().clear();
						});
						
				},
					
				props:function(fun){
					var buttonArray = ["<input name='' type='button' value='开 局'  class='pokeyButton button-blue' id='button-start'/>",
					                   "<input name='' type='button' value='道 具'  class='pokeyButton button-blue' id='button-props'/>"
					                  ];
					
					
					//GameModeType.MODE_TYPE_BOSS = 3;
					//GameModeType.MODE_TYPE_BEAN_FUN_BATTLE = 4;
					var gameModeType = dataStorage.get(OtherThing.gameModeType);
					if(gameModeType == GameModeType.MODE_TYPE_BOSS  || gameModeType == GameModeType.MODE_TYPE_BEAN_FUN_BATTLE){
						var b = buttonArray[0]+ buttonArray[1];	
					}else{
						openGame();
						return;
					}
					
					_t.pokeyButton.html(b);
					player.countdown("my",fun);
					
					var startButton = $("#button-start"),propsButton = $("#button-props");
					
						
						startButton.click(function(){
							openGame();
							Video.ready();
							_t.button().clear();
						});
						
						require(["js/mProps.js"], function(props){ 
							propsButton.click(function(){
								props.roomList();
								propsButton.hide();
							});
						});
				
					
					
				},
					
					
				/*叫地主*/
				bid:function(n, fun,timeOut){
					var n = n || 0;
					var buttonArray = ["<input name='' type='button' value='不 叫'  class='pokeyButton button-blue' id='button-no'/>",
									   "<input name='' type='button' value='1分' num='1'  class='pokeyButton button-blue' id='button-one'/>",
									   "<input name='' type='button' value='2分' num='2' class='pokeyButton button-blue' id='button-two'/>",
									   "<input name='' type='button' value='3分' num='3' class='pokeyButton button-blue' id='button-three'/>"];
					
					var b=buttonArray[0]+ buttonArray[1]+ buttonArray[2]+ buttonArray[3];
					_t.pokeyButton.html(b);
					var buttonNo = $("#button-no");
					var buttonOne = $("#button-one");
					var buttonTwo = $("#button-two");
					var buttonThree = $("#button-three");
					if(n==0){
						//b = buttonArray[0]+ buttonArray[1]+ buttonArray[2]+ buttonArray[3];
						buttonNo.addClass("button-yellow-1");
						buttonOne.addClass("button-blue-1");
						buttonTwo.addClass("button-blue-1");
						buttonThree.addClass("button-blue-1");
					}else if(n == 2){
						//b = buttonArray[0]+ buttonArray[2]+ buttonArray[3];	
						buttonNo.addClass("button-yellow-1");
						buttonOne.addClass("button-gay-1").attr("id","");
						buttonTwo.addClass("button-blue-1");
						buttonThree.addClass("button-blue-1");
					}else if(n == 3){
						//b = buttonArray[0]+ buttonArray[3];	
						buttonNo.addClass("button-yellow-1");
						buttonOne.addClass("button-gay-1").attr("id","");
						buttonTwo.addClass("button-gay-1").attr("id","");
						buttonThree.addClass("button-blue-1");
					}
					
					player.countdown("my",fun);
					
					$("#button-no").click(function(){
							_t.button().clear();
							var num = $(this).attr("num");
							gameOnGrabLandLord(num);
							console.log("Q-->你点击“不叫”按钮");
						});
						
						/*1分*/
						
					$("#button-one").click(function(){
							_t.button().clear();
							var num = $(this).attr("num");
							gameOnGrabLandLord(num);
							console.log("Q-->你点击“1分”按钮");
						});
						
						
						/*2分*/
						
					$("#button-two").click(function(){
							_t.button().clear();
							var num = $(this).attr("num");
							gameOnGrabLandLord(num);
							console.log("Q-->你点击“2分”按钮");
						});
						
						/*3分*/
						
					$("#button-three").click(function(){
							_t.button().clear();
							var num = $(this).attr("num");
							gameOnGrabLandLord(num);
							console.log("Q-->你点击“3分”按钮");
						});
					
					
					
					},
					
					
				/*加倍*/
				pull:function(playerId,fun,timeout){
					
					var buttonArray = ["<input name='' type='button' value='加 倍'  class='pokeyButton button-blue' id='button-pull'/>",
									   "<input name='' type='button' value='不加倍'  class='pokeyButton button-blue' id='button-nopull'/>"];
					
					var b = buttonArray[0]+ buttonArray[1];	
					_t.pokeyButton.html(b);
					player.countdown("my",fun);
					
					$("#button-pull").click(function(){
							_t.button().clear();
							gameDouble(true,playerId);
							console.log("Q-->你点击“加倍”按钮");
						});
						
						
					$("#button-nopull").click(function(){
							_t.button().clear();
							gameDouble(false,playerId);
							console.log("Q-->你点击“不加倍”按钮");
						});
				
					
					
					},
					
					
				/*出牌阶段 出 不出 提示
				* isMustPut 是否必出  把“不出”按钮隐藏
				*/
				put:function(isMustPut,fun){
					var isMustPut = isMustPut || false;
					var buttonArray = ["<input name='' type='button' value='不 出'  class='pokeyButton button-blue' id='button-noput'/>",
									   "<input name='' type='button' value='重 选'  class='pokeyButton button-blue' id='button-reSelect'/>",
									   "<input name='' type='button' value='提 示'  class='pokeyButton button-blue' id='button-prompt'/>",
									   "<input name='' type='button' value='出 牌'  class='pokeyButton button-blue' id='button-put'/>"
									  ];
					
					var b = buttonArray[0]+ buttonArray[1]+ buttonArray[2]+ buttonArray[3];
					_t.pokeyButton.html(b);
					var buttonNoput = $("#button-noput");
					var buttonPrompt = $("#button-prompt");
					var buttonReSelect = $("#button-reSelect");
					var buttonPut = $("#button-put");
					if(isMustPut){
						//b =buttonArray[1]+ buttonArray[2]+ buttonArray[3];
						buttonNoput.addClass("button-gay-1").attr("id","");
						buttonPrompt.addClass("button-blue-1");
						buttonReSelect.addClass("button-blue-1");
						buttonPut.addClass("button-blue-1");
					}else{
						//b = buttonArray[0]+ buttonArray[1]+ buttonArray[2]+ buttonArray[3];
						buttonNoput.addClass("button-yellow-1");
						buttonPrompt.addClass("button-blue-1");
						buttonReSelect.addClass("button-blue-1");
						buttonPut.addClass("button-blue-1");
					}
					
					
					player.countdown("my",fun);
					
					var clickNum = 0;
					var timeStamp = new Date().getTime();
					
					
					 
						
					$("#button-noput").click(function(){
							gameOnNotOutCard(); // 不出牌
							 _t.button().clear();
							 player.msg("my","text-noPut");
							 _t.reSelectPokey();
							 console.log("Q-->你点击“不出牌”按钮");
						});
						
						/*提示*/
						
						
					$("#button-prompt").click(function(){
							gameOnRequestOutCardSussestion(clickNum,timeStamp);
							clickNum++;
							console.log("Q-->你点击“提示”按钮");
						});
						
						
						/*重选*/
						
					$("#button-reSelect").click(function(){
							_t.reSelectPokey();
							console.log("Q-->你点击“重选”按钮");
						});
						
						
						/*出牌*/
						
					$("#button-put").click(function(){
							_t.put(isMustPut);
							Video.put();
							console.log("Q-->你点击“出牌”按钮");
						});
						
					},
					
				
				
				/*清空*/
				clear:function(){
					_t.pokeyButton.html("");
					player.hideCountdown();
					}
					
				}
				
				
				
			},
			
			
			/*提示时给出选择牌
			 * 把所有的牌初始化 
			 * 选择要提示的牌
			 * */
			selectPokey:function(arr){
				this.pokeyBox.find(".card").attr("isSelect",0);
				this.pokeyBox.find(".card").css({"top":0});
				for(var i=0; i<arr.length; i++){
					$("#"+arr[i]).attr("isSelect",1);
					$("#"+arr[i]).css({"top":-20});
					}
				},
				
			/*重选
			 * 把所有的牌初始化
			 * */
			reSelectPokey:function(){
				this.pokeyBox.find(".card").attr("isSelect",0);
				this.pokeyBox.find(".card").css({"top":0});
				},
				
				
			/*托管选择一手牌
			 * 选择要出的牌
			 * 删除已选择的牌 this._removePut();
			 * 清空按钮this.button().clear();
			 * 设置牌的间距this._setPokeyMargin();
			 * 显示出了牌在桌面上 player.put("my",arr);
			 * */
			trusteePut:function(arr){
				this.reSelectPokey();
				var _t = this;
				for(var i=0;i<arr.length; i++){
					var y = function(n){
						$("#"+arr[n]).attr("isSelect",1);
						$("#"+arr[n]).css({"top":-20});
					}(i)
				}
				
				this._removePut();
				this.button().clear();
				this._setPokeyMargin();
				player.put("my",arr);
				console.log("Q-->托管选择一手牌");
			},
			
			
			
			/************************************私有方法*****************************************/
			
		
		/*创建手牌
		 * 生成DOM节点
		 * 设置透明度
		 * 设置牌的间距this._setPokeyMargin();
		 * 设置三张背景底牌的位置 this._startSetPost();
		 * */
			_createPokey:function(arr){
					var html="";
					console.log("Q-->你得到"+arr.length+"张牌,他们是"+arr);
					for(var i=arr.length-1; i>=0; i--){
						var type = pokeyArray[arr[i]%4];
						var num = pokeyArrayNum[Math.floor(arr[i]/4)];
						
						if(parseInt(arr[i]) === 53){
							html+="<span id='"+arr[i]+"' class='card play_icon smallKing' style='z-index:"+(100-arr[i])+"' isSelect='0'></span>";
						}else if(parseInt(arr[i]) === 56){
							html+="<span id='"+arr[i]+"' class='card play_icon bigKing' style='z-index:"+(100-arr[i])+"' isSelect='0'></span>";
						}else{
							html+="<span id='"+arr[i]+"' class='card play_icon "+type+"' style='z-index:"+(100-arr[i])+"' isSelect='0'><span class='cardFont'>"+num+"</span></span>";
						}
						}
					//console.log(html)
					/*插入并设置透明度为0*/
					this.pokeyBox.html(html).find(".card").css({
						"opacity":0										   
					});
					
					
				},
				
				
			/*创建三张底牌*/
			_createBottomPokey:function(arr){
				var _t = this;
				
				for(var i=0; i<arr.length; i++){
					var y = function(n){
						var insertArr = _t._insertPosition(arr[n]);
						
						var type = pokeyArray[arr[i]%4];
						var num = pokeyArrayNum[Math.floor(arr[i]/4)];
						
						if(parseInt(arr[i]) === 53){
							var html="<span id='"+arr[i]+"' class='card  play_icon smallKing' style='z-index:"+(100-arr[i])+"' isSelect='0'></span>";
						}else if(parseInt(arr[i]) === 56){
							var html="<span id='"+arr[i]+"' class='card  play_icon bigKing' style='z-index:"+(100-arr[i])+"' isSelect='0'></span>";
						}else{
							var html="<span  id='"+arr[i]+"' class='card play_icon "+type+"' style='z-index:"+(100-arr[i])+"' isSelect='0' ><span class='cardFont'>"+num+"</span></span>";
							}
						
						if(insertArr[0] === "after"){
							$("#"+insertArr[1]).after(html);
						}else{
							$("#"+insertArr[1]).before(html);
							}
					}(i)
				}
			},
				
			/*开始时设置牌的位置&&底牌背景的位置
			 * 生成背景底牌的DOM节点this._createPokeyBg();
			 * 设置背景底牌的位置
			 * 把所以的牌都定位到底牌的位置【通过动画形式分发到目标位置】*/
			_startSetPost:function(isNoReGet){
				var cards = this.pokeyBox.find(".card");
				/*创建一个背景的底牌 并设置到中央位置*/
				this._createPokeyBg();
				this._pokeyBg.css({
							"top":"30%",
							"left":"45%"
							});
				if(!isNoReGet){
					var ox = this._pokeyBg.offset().left;
					var oy =  this._pokeyBg.offset().top;
					var t = cards.eq(0).offset().top;
					var _t = this;
					/*把所以的牌都定位到中央位置*/
					for(var i=0; i<cards.length; i++){
							var l =  cards.eq(i).offset().left;
							var left = ox-l;
							cards.eq(i).css({
								top:oy-t,
								left:left
							});
						}
				}
				
				},
				
			/*设置手牌的间距 牌少就间距大 牌多就间距小*/
			_setPokeyMargin:function(){
				var cards = this.pokeyBox.find(".card");
				var length = cards.length;
				var cardWidth = parseInt(cards.css("width"));
				var _t = this;
				setTimeout(function(){
					//var w = Screen.size()[0]*$("#scene").attr("scale");
					//console.log("0----->"+w);
					var oMargin = (Screen.size()[0]-cardWidth)/(length-1);
					console.log("--->"+oMargin+"-->"+Screen.size()[0]);
					oMargin = Math.ceil(cardWidth - oMargin);
					oMargin = oMargin>0 ? oMargin : 0;
					console.log("--->"+oMargin);
					_t.pokeyBox.find(".card").css({
							"margin-left":-oMargin/2,	
							"margin-right":-oMargin/2
						})
					},0)
				
				},
				
			/*动画显示手牌*/
			_animationShowPoker:function(){
				var _t = this;
				var cards = this.pokeyBox.find(".card");
				for(var i=0; i<cards.length; i++){
					var y = function(n){
						setTimeout(function(){
							_t.pokeyBox.find(".card").eq(n).animate({
									"opacity":100,
									"top":0,
									"left":0
							},100);
						},50*n);
					}(i)
				}
				
				setTimeout(function(){
						_t._animationShowThreePoker();		
				},50*17)
				},
				
			/*底牌背景*/
			_createPokeyBg:function(){
				var h = "<div  class='threeCard cardBg play_icon card-amid'></div>";
				$("#playBox").append(h+h+h);
				this._pokeyBg = $(".threeCard");
				},
			
			/*动画展开三张底牌*/	
			_animationShowThreePoker:function(){
				this._pokeyBg.eq(0).animate({
								"left":"35%"
							},50);
				
				this._pokeyBg.eq(2).animate({
								"left":"55%"
							},50);
				},
				
			/* 得到要插入牌的位置*/
			_insertPosition:function(n){
				for(var i = this.pokeyBox.find(".card").length-1; i>=0; i--){
					var num = parseInt(this.pokeyBox.find(".card").eq(i).attr("id"));
					
					/*返回位置*/
					if(n<num){
						return ["after",num];
						}
						
					/*放最后一张*/	
					if(n>parseInt(this.pokeyBox.find(".card").first().attr("id"))){
						return  ["before",parseInt(this.pokeyBox.find(".card").first().attr("id"))];												 
						}
					}
				},
				
			/*设置三张底牌的插入时的初始位置*/
			_setBottomPokerPost:function(arr){
				var _t = this;
				for(var i=0; i<arr.length; i++){
					var y = function(n){
						var obj = $("#"+arr[n]);
						var l = _t._pokeyBg.eq(n).offset().left;
						var t =  _t._pokeyBg.eq(n).offset().top;
						var ol = obj.offset().left;
						var ot = obj.offset().top;
						obj.css({
							top:t-ot,
							left:l-ol
							});
						obj.animate({"top":0,left:0},200);
						}(i);
					}
				},
				
		
				
			/*得到手牌 对手牌进行点击和触摸滑动事件处理*/
			_bindEvent:function(){
		
				var _t = this;
				//var isIphone = /i(Phone|P(o|a)d)/.test(navigator.userAgent);
				this.SUPPORTTOUCH = document.hasOwnProperty("ontouchstart"); 
				var getEvent=this.SUPPORTTOUCH?["touchstart","touchend","touchmove"] : ["mousedown","mouseup","mousemove"]
				_t.isTouch = false;
				
				
				/*body取消默认触摸滑屏事件*/
				//var pokeyBox = document.getElementById("pokeyBox");
				//pokeyBox.addEventListener
				var pokeyBox = $("#pokeyBox>.card");
				pokeyBox.live(getEvent[0], function(e) {
					
					 _t._touchStart(e);     
				});
				
				pokeyBox.live(getEvent[2], function(e) { 
					e.stopPropagation();
					e.preventDefault();
					 _t._touchMove(e);   
				});
				
				pokeyBox.live(getEvent[1], function(e) { 
					_t._touchEnd(e);    
				});
				
				$("body").live("touchmove",function(e){
					e.preventDefault();
					e.stopPropagation();
				});
				
				$("body").click(function(e){
					if($(e.target).hasClass("playBox") || $(e.target).hasClass("myButton")){
						_t.reSelectPokey();
					}
					
				})
				
				/*点击头像查看信息*/
				$(".headIcon").live("click",function(){
						
					
						if($(this).isChildOf(".left")){
							player.showHead("left");
							}
							
						if($(this).isChildOf(".right")){
							player.showHead("right");
							}
							
						if($(this).isChildOf(".my")){
							player.showHead("my");
							}
				});
				
				},
				
			
			_click:function(t){
				var _t = this;
				if(t.attr("isSelect") != 0){
					_t._dowmAnim(t);
					}else{
					_t._upAnim(t);
				}
			},
				
			_touchStart:function(e){
				
				var _t = this;
				_t.isTouch = true;
				
				var pokeyBox = this.pokeyBox.find(".card");
				if(pokeyBox.length===0){
					_t.isTouch = false;
					return;
				}
				var x = _t.SUPPORTTOUCH? e.touches[0].pageX : e.pageX ;
				var y = _t.SUPPORTTOUCH? e.touches[0].pageY : e.pageY ;
				var boxLeft = parseInt(pokeyBox.eq(0).offset().left);
				var boxTop = parseInt(pokeyBox.offset().top)-20;
				var boxHeight = parseInt(pokeyBox.css("height"));
				console.log("_touchStart:"+y+"-->boxTop:"+boxTop);
				this.selectMinNum = this._touchMoveSelectNum(boxLeft,x,y,boxTop,boxHeight);
				_t.pokeyBox.find(".card").eq(this.selectMinNum).addClass("on");
				
			},	
				
				
			/*触摸结束时*/	
			_touchEnd:function(e){
				var _t = this;
				_t.isTouch = false;
				var isUp = _t._isUpCard();
				if(typeof _t.maxNum !== "undefined"){
					
					for(var i=_t.minNum; i<=_t.maxNum; i++){
						var y = function(n){
								_t._seletCard(n);
							}(i)
						}
						_t.selectMinNum = undefined;
						_t.selectMaxNum = undefined;
						_t.maxNum  = undefined;
						_t.minNum = undefined;
				}else{
					_t._seletCard(_t.selectMinNum);
				}
				console.log("所以选中的牌："+this._getPut()+":所有的牌："+this._getAllPut());
				if(isUp){
					require(["js/mRule.js"], function(rule){
						var arr = rule.sAI(_t._getPut(),_t._getAllPut());
						if(arr && arr.length>4){
							_t.selectPokey(arr);
						}
					});
				}
				
				//$(".baseScore").append("dowm-"+minNum+":"+maxNum);
			},
				
				
				/*触摸结束时*/	
				_touchMove:function(e){
					//$(".baseScore").append(this.isTouch);
					if(this.isTouch){
						console.log("_touchMove");
						var _t = this;
						var pokeyBox = this.pokeyBox.find(".card");
					
						var x = _t.SUPPORTTOUCH? e.changedTouches[0].pageX : e.pageX ;
						var y = _t.SUPPORTTOUCH? e.changedTouches[0].pageY : e.pageY ;
						var boxLeft = parseInt(pokeyBox.eq(0).offset().left);
						var boxTop = parseInt(pokeyBox.offset().top)-20;
						var boxHeight = parseInt(pokeyBox.height());
						this.selectMaxNum = this._touchMoveSelectNum(boxLeft,x,y,boxTop,boxHeight);
						
						if(_t.selectMinNum>_t.selectMaxNum){
							_t.minNum = _t.selectMaxNum;
							_t.maxNum = _t.selectMinNum;
						}else if(_t.selectMinNum<_t.selectMaxNum){
							_t.minNum = _t.selectMinNum;
							_t.maxNum = _t.selectMaxNum;
						}else{
							return ;
						}
						
						_t.pokeyBox.find(".card").removeClass("on");
						_t.pokeyBox.find(".card").slice(_t.minNum,_t.maxNum+1).addClass("on");
					}
					
				},
				
				
				
				_seletCard:function(n){
					var _t = this;
					var scard =   _t.pokeyBox.find(".card").eq(n);
					
					_t.pokeyBox.find(".card").removeClass("on");
					if( scard.attr("isSelect") != 0){
						_t._dowmAnim(scard);
					}else{
						_t._upAnim(scard);
					};
				},
				
				
				_isUpCard:function(){
					var _t = this;
					if(typeof _t.maxNum !== "undefined"){
						for(var i=_t.minNum; i<=_t.maxNum; i++){
							var scard =   _t.pokeyBox.find(".card").eq(i);
							if( scard.attr("isSelect") != 0){
								return false
							}
						}
						return true
					}else{
						var scard =   _t.pokeyBox.find(".card").eq(_t.selectMinNum);
						if( scard.attr("isSelect") != 0){
							return false;
						}else{
							return true;
						}
					}
					
					
				},
				
				
				
				/*
				获取牌的属性 私有属性
				_firstPokey 第一张牌
				_pokeyWidth 牌的宽度
				_pokeyRight 牌的margin-right
				_pokeyDistance 牌的距离
				
				*/
				_proto:function(){
					this._firstPokey = this.pokeyBox.find(".card").eq(0);
					this._touchMoveArr = [];
					this.selectMinNum=null,this.selectMaxNum=null;
					
					},
					
					
					
				
				/*
				向上升起 并设置已选中
				参数N  -- 第N张牌
				*/
				_upAnim:function( t ){
					//$(".baseScore").append("up-");
					t.css({"top":-20});
					t.attr("isSelect",1);
					
					},
					
				/*
				下降 并设置未选中
				参数N  -- 第N张牌
				*/
				_dowmAnim:function(t){
					//$(".baseScore").append("dowm-");
					t.css({"top":0});
					t.attr("isSelect",0);
					
					},
					
					
				
				//判断触摸滑动时 是否选中了牌  如果选中了  就返回TRUE 
				_touchMoveSelectNum:function(l,x,y,t,h){
					var scale =  $("#scene").attr("scale");
					x = parseInt(x/scale);
					l = parseInt(l/scale);
					console.log(l);
					//y= parseInt(y/scale);
					//t = parseInt(t/scale);
					//h = parseInt(h/scale);
					console.log("选中的牌的坐标："+x+"--->"+y+"--->"+t+"--->"+h);
					//选中的牌的坐标：466.9020866773676--->380.6099518459069--->440--->159 
					if(y<t || y>(t+h+20)){
						return ;
					}
					
					if(x-l<0){
						return ;
					}	
					var pokeyBox = this.pokeyBox.find(".card");
					var pokeyWidth = parseInt(pokeyBox.css("width"));
					var pokeyRight = Math.abs(parseFloat(pokeyBox.css("margin-right")));
					var pokeyLeft = Math.abs(parseFloat(pokeyBox.css("margin-left")));
					
					var pokeyDistance = pokeyWidth-pokeyRight-pokeyLeft;
					var  n = Math.floor((x - l)/pokeyDistance) >= pokeyBox.length? pokeyBox.length-1 : Math.floor((x - l)/pokeyDistance);
					console.log("选中的牌："+n);
					return n;
					},
					
					
				//得到要出的牌
				_getPut:function(){
					var length = this.pokeyBox.find(".card").length;
					var arr = [];
					for(var i=0; i<length; i++){
						var n =  this.pokeyBox.find(".card").eq(i).attr("isSelect");
						if(n === "1"){
							arr.unshift(this.pokeyBox.find(".card").eq(i).attr("id"));
							}
						}
					return arr;
					},
					
				/*删除出掉的牌*/
				_removePut:function(){
					var length = this.pokeyBox.find(".card").length;
					for(var i=0; i<length; i++){
						var n =  this.pokeyBox.find(".card").eq(i).attr("isSelect");
						if(n === "1"){
							this.pokeyBox.find(".card").eq(i).remove();
							i--;
							}
						}
					},
					
				_getAllPut:function(){
					var length = this.pokeyBox.find(".card").length;
					this._allCardArr = [];
					for(var i=length-1; i>=0; i--){
						var n =  this.pokeyBox.find(".card").eq(i).attr("id");
						this._allCardArr.push(n);
					}
					return this._allCardArr;
				}
					
		}
		
		

/*三个人都公用的方法
1、倒计时  {显示设置 countdown(obj,n), 隐藏 hideCountdown}
2、谈话	 {显示设置 talk(obj,text), 隐藏 hideTalk}
3、信息设置 
   [setInfo().head(obj,isLandlord)头像，
	setInfo().multiple(obj,num)倍数，
	setInfo().name(obj,name) 姓名，
	setInfo().dou(obj,num) 乐豆，
	setInfo().pokeyNum(obj,num) 牌数]
   
   隐藏信息
   hideInfo()
   显示信息
   showInfo()
   
   
4、设置基分{
	setBaseScore(num)显示设置
	hideBaseScore()隐藏
	}
	
5、出牌 put{
		obj 对象
		arr 牌的数组
	}
	
6、显示头像 showHead
7、游戏玩家进入 goin{ obj}
8、提示信息 msg(obj,text) hideMsg();
9、准备 ready(obj)

*/
var player = {
			
			/*游戏玩家进入
			 * 创建头像信息this._createInfo(obj,info);
			 * 设置名字this.setInfo().name(obj,name);
			 * 设置豆this.setInfo().dou(obj,dou);
			 * */
			goin:function(obj,info){
				console.log(info)
				var name = info.name;
				var dou = info.dou;
				var star = info.star;
				var candy = info.candy;
				var score = info.score;
				this._createInfo(obj,info);
				this.setInfo().name(obj,name);
				this.setInfo().dou(obj,dou);
				this.setInfo().star(obj,star);
				this.setInfo().candy(obj,candy);
				this.setInfo().score(obj,score);
				this.setInfo().multiple(obj, 1);
				
				console.log("Q-->"+obj+"--->进入房间,他的信息是"+info);
				
				},
				
			/*准备
			 * 显示准备信息this.okMsg(obj);*/	
			ready:function(obj){
				this.okMsg(obj);
				console.log("Q-->"+obj+"--->准备");
				},
			
			/*要出的牌
			 * 创建要出牌 this._createPutPokey(arr,box);
			 * 判断 如果是自己 牌居中
			*/
			put:function(obj,arr){
				var box = $("."+obj+" .putpokeyBox");
				this._createPutPokey(arr,box);
				pokeyEffect.anmi(arr,obj);
				if(obj === "my"){
					box.css("margin-left",-parseInt(box.css("width"))/2)
					}
				console.log("Q-->"+obj+"--->要出的牌"+arr);
				},
				
				/*要出的牌
				 * 创建要出牌 this._createPutPokey(arr,box);
				 * 判断 如果是自己 牌居中
				*/
			putOther:function(obj,arr){
					this.setInfo().pokeyNum(obj,0);
					var box = $("."+obj+" .putpokeyBox");
					this._createPutPokey(arr,box);
					console.log("Q-->"+obj+"--->要出剩余的手牌"+arr);
					if(obj === "my"){
						box.css("margin-left",-parseInt(box.css("width"))/2)
						}
					},
			
			/*清空手牌*/
			clearPut:function(obj){
				var box = $("."+obj+" .putpokeyBox");
				box.html("");
				console.log("Q-->"+obj+"--->清空手牌");
				},
				
			/*清空手牌*/
			clearMyPokey:function(){
				var box = $("#pokeyBox");
				box.html("");
			},
			
			/*倒计时 
			 * fun 是倒计时结束之后的回调函数
			 * 生成DOM节点
			 * 开始倒计时this._waitCountdown(fun,n);
			*/	
			
			countdown:function(obj,fun,n){
				var n = n || 30;
				if($(".wait")){
					$(".wait").remove();
					}
				clearTimeout(this.waitTimer);
				var html = "<div class='wait play_icon "+obj+"Wait'>"+n+"</div>";
				if(obj === "props"){
					$("#propsTimer").append(html);
				}else{
					$("#playBox").append(html);
				}
				
				this._waitCountdown(fun,n);
				console.log("Q-->"+obj+"--->倒计时");
				},
				
				
			/*隐藏倒计时*/
			hideCountdown:function(){
				if($(".wait")){
					$(".wait").remove();
					}
				if(this.waitTimer){
					clearTimeout(this.waitTimer);
				}
				},
				
			/*谈话信息
			 * 生成DOM节点
			 * 在5秒后删除*/	
			talk:function(obj,text){
				clearTimeout(this._talkTimer);
				if($(".talk")){
					$(".talk").remove();
					}
				var html = "<div class='talk "+obj+"Talk'>"+text+"</div>";
				$("#playBox").append(html);
				
				this._talkTimer = setTimeout(function(){
					$(".talk").remove();				
					},5000);
				},
				
			/*隐藏所有谈话*/	
			hideTalk:function(){
				if($(".talk")){
					$(".talk").remove();
					}
				},
				
			/*设置信息*/	
			setInfo:function(){
				return {
					/*设置头像
					obj 对象
					isLandlord 是否是地主
					*/
					head:function(obj,isLandlord){
						var headIcon = $("."+obj+" .info");
						if(isLandlord){
							headIcon.addClass("landlord");
							}else{
							headIcon.removeClass("landlord");
							}
						},
					
					/*设置倍数
					obj 对象
					num 倍数
					*/
					multiple:function(obj,num){
						
						var headIcon = $("."+obj+" .pokeyMultiple");
						require(["js/mCard.js"], function(card){ 
							var allBombNums = dataStorage.get(OtherThing.bombNums) || 0;
							headIcon.attr("num",num);
							if(obj === "right"){
								dataStorage.set(RightPlayer.multiple, num);
							}else if(obj ==="left"){
								dataStorage.set(LeftPlayer.multiple, num);
							}else if(obj === "my"){
								dataStorage.set(MyOwner.multiple, num);
								var all = Math.pow(2,allBombNums) * num;
								$("#pokeyMyBox .pokeyMultiple").show().html(card.getNumGif("x"+all,"blue"));
							}
							
							
							setTimeout(function(){
								if(dataStorage.get(MyOwner.isLandlord)==="true"){
									var left = dataStorage.get(LeftPlayer.multiple);
									var right = dataStorage.get(RightPlayer.multiple);
									
									left = parseInt(left)?parseInt(left):1;
									right =  parseInt(right)?parseInt(right):1;
									if(left === 1){
										var my = right;
									}else if(right === 1){
										var my = left;
									}else{
										var my= left+right;
									}
									
									dataStorage.set(MyOwner.multiple, my);
									var all = Math.pow(2,allBombNums) * my;
									$("#pokeyMyBox .pokeyMultiple").show().html(card.getNumGif("x"+all,"blue"));
								}
							},500);
						});	
						
						
						},
						
						
					/*设置姓名
					obj 对象
					name 姓名
					*/
					name:function(obj,name){
						var pokeyName = $("."+obj+" .pokeyName");
						pokeyName.html(name);
						},
						
					/*设置积分
					obj 对象
					name 姓名
					*/
					score:function(obj,num){
						var pokeyScore = $("."+obj+" .pokeyScore");
						pokeyScore.html(num);
					},
						
					/*设置豆数
					obj 对象
					num 豆数
					*/
					dou:function(obj,num){
						var pokeyDou = $("."+obj+" .pokeyDou");
						require(["js/mCard.js"], function(card){  
							pokeyDou.html(card.getNumGif(num,"blue"));
							pokeyDou.attr("num",num);
						});
						},
						
					/*设置星星
					obj 对象
					num 豆数
					*/
					star:function(obj,num){
						var pokeyXing = $("."+obj+" .pokeyXing");
						require(["js/mCard.js"], function(card){  
							pokeyXing.html(card.getNumGif(num,"blue"));
							pokeyXing.attr("num",num);
						});
						},
							
					/*设置星星
					obj 对象
					num 豆数
					*/
					candy:function(obj,num){
						var pokeyTang = $("."+obj+" .pokeyTang");
						require(["js/mCard.js"], function(card){  
							pokeyTang.html(card.getNumGif(num,"blue"));
							pokeyTang.attr("num",num);
						});
						},
						
					/*设置牌数
					obj 对象
					num 豆数
					*/
					pokeyNum:function(obj,num){
						var pokeyNum = $("."+obj+" .pokeyNum .pokeyNum-num");
						if(num<=3 && num>0){
							pokeyNum.addClass("alarmAnmi");
						}else{
							pokeyNum.removeClass("alarmAnmi");
						}
						pokeyNum.html(num);
						},
						
					peopleHead:function(obj,roleName,peopleHeadNum){
						var img = $("."+obj+" .headIcon img");
						img.attr("src","images/xy/"+roleName+"/logo.png");
						
					}
						
				}
			},
				
			/*获取信息*/
			getInfo:function(){
				return {
					name:function(obj){
						var pokeyName = $("."+obj+" .pokeyName");
						return pokeyName.html();
						},
					
					nameCode:function(dou){
						var name
						require(["js/mCard.js"], function(card){ 
							name =  card.getName(dou);
							
						});
						return ""
					},
						
					score:function(obj){
							var pokeyScore = $("."+obj+" .pokeyScore");
							return pokeyScore.html();
							},
					
					dou:function(obj){
						var pokeyDou = $("."+obj+" .pokeyDou");
						return pokeyDou.attr("num");
						},
						
					star:function(obj){
						var pokeyDou = $("."+obj+" .pokeyXing");
						return pokeyDou.attr("num");
					},
					
					candy:function(obj){
						var pokeyDou = $("."+obj+" .pokeyTang");
						return pokeyDou.attr("num");
					},
					
					multiple:function(obj){
						var multiple = $("."+obj+" .pokeyMultiple");
						
						return multiple.attr("num");
				
					}
				}
				},
				
			/*隐藏信息*/
			hideInfo:function(){
					return {
						/*整个头像*/
						all:function(obj){
							$("."+obj).hide();
						},
						
						/*倍数*/
						multiple:function(obj){
							var pokeyMultiple = $("."+obj+" .pokeyMultiple");
							pokeyMultiple.hide();
							}
						}
						
					},
				
				
			/*显示信息*/	
			showInfo:function(obj){
				$("."+obj).show();
				},
			
			/*设置显示基分*/
			setBaseScore:function(num){
				$(".baseScore").show().html("基分："+num);
				console.log("Q-->显示基分"+num);
				},
				
			/*隐藏基分*/
			hideBaseScore:function(num){
				$(".baseScore").hide();
				},
				
			/*显示头像*/
			showHead:function(obj){
				
				var name = this.getInfo().name(obj);
				var dou = this.getInfo().dou(obj);
				
				require(["js/mCard.js"], function(card){ 
					if($(".headDiv").length){
						$(".headDiv").remove();
						return;
					}
						
					var html = "<div class='headDiv  "+obj+"HeadDiv'>\
									<ul>\
										<li >昵称："+name+"</li>\
										<li >金币："+dou+"</li>\
										<li >财富："+card.getName(dou)+"</li>\
									</ul>\
								</div>";
								
					$("#playBox").append(html);
				});
				
				$(document).click(function(){
					if(!$(event.target).isChildOf(".headDiv") && !$(event.target).isChildAndSelfOf(".headIcon")){
							$(".headDiv").hide();
						}			   
					})
					
				},
				
				
				/*显示弹出信息
				* 1准备 text-ready
				* 2加倍  text-double
				* 3不加倍 text-noDouble
				* 4不出 text-noPut
				* 5我又掉线了 text-downline
				* 6没有大过的牌型 text-noBig
				* 7 出牌不符合规则  text-noAccord
				* 8无人抢地主，重新发牌 text-noGrab
			* */
				msg:function(obj,text){
					if($("."+obj+"Msg").length === 1){
						$("."+obj+"Msg").remove();	
						}
					
					var html = "<div class='msg "+obj+"Msg'><span class='num_icon "+text+"'></span></div>";
					$("#playBox").append(html);
					},
					
				/*隐藏弹出信息*/	
				hideMsg:function(obj){
					$("."+obj+"Msg").remove();
				},
				
				
				/*显示弹出信息
				* 1准备 text-ready
				* 2加倍  text-double
				* 3不加倍 text-noDouble
				* 4不出 text-noPut
				* 5我又掉线了 text-downline
				* 6没有大过的牌型 text-noBig
				* 7 出牌不符合规则  text-noAccord
				* 8无人抢地主，重新发牌 text-noGrab
			* */
				systemMsg:function(text){
					clearTimeout(this.systemTimer||0);
					var system = $(".systemMsg");
					if(system.length === 1){
						system.remove();	
					}
					var html = "<div class='systemMsg'><span class='num_icon "+text+"'></span></div>";
					$("#playBox").append(html);
					var _t = this;
					this.systemTimer = setTimeout(function(){
						_t.hideSystemMsg();
					},3000)
				},
				
				hideSystemMsg:function(){
					$(".systemMsg").animate({
	        			"opacity":0
	        		},300);
	    		},
					
				
				
				
				/*显示OK准备信息*/
				okMsg:function(obj){
					var html = "<div class='okMsg "+obj+"OkMsg'><span class='num_icon text-ready'></span></div>";
					$("#playBox").append(html);
					},
					
				hideOkMsg:function(){
					$(".okMsg").remove();
					},
					
					
				/*显示托管*/
				showTrusteeship:function(obj){
					if(obj === "my"){
						this._myTrusteeship();
						
					}else{
						this.msg(obj,"托管");
					}
					},
					
				hideTrusteeship:function(obj){
					if(obj === "my"){
						this._hideMyTrusteeship();
					}else{
						this.hideMsg(obj);
					}
					},
					
					
				/*动画显示加分和减分*/
				showAnimDou:function(obj){
					var rD = obj.rightWinChipsCount;
					var lD = obj.leftWinChipsCount;
					var mD = obj.myWinChipsCount;
					require(["js/mCard.js"], function(card){  
						var html = "<div class='animDou leftAnimDou'>"+card.getNumGif(lD,'blue')+"</div>" +
							"<div class='animDou rightAnimDou'>"+card.getNumGif(rD,'blue')+"</div>"+
							"<div class='animDou myAnimDou'>"+card.getNumGif(mD,'blue')+"</div>";
						$("#playBox").append(html);
						$(".leftAnimDou,.rightAnimDou").animate({
							"top":"25%"
						},1500);
						
						$(".myAnimDou").animate({
							"bottom":"40%"
						},1500);
						
					});
					
					
					
				},
				
				clearAnimDou:function(){
					$(".animDou").remove();
				},
					
				
			
				
				
				
	/******************************私有方法****************************************/
				
			/*开始倒计时*/	
			_waitCountdown:function(fun,n){
				var _t = this;
				var countDownNum = n;
				timer();
				function timer(){
					_t.waitTimer = setTimeout(function(){
						if(countDownNum>1){
							clearTimeout(_t.waitTimer);
							countDownNum--;
							$(".wait").html(countDownNum);
							if(countDownNum<6){
								$(".wait").addClass("alarmAnmi");
							}else{
								$(".wait").removeClass("alarmAnmi");
							}
							
							timer();
							
						}else{
							clearTimeout(_t.waitTimer);
							$("#myButton").html("");
							_t.hideCountdown();
							if(fun){
								fun()
							}
							
						}			
					},1000);
				}
				},
				
				
			_createPutPokey:function(arr,box){
				var html="";
				for(var i=arr.length-1; i>=0; i--){
						var type = pokeyArray[arr[i]%4];
						var num = pokeyArrayNum[Math.floor(arr[i]/4)];
						
						if(parseInt(arr[i]) === 53){
							html+="<span  class='card card-amid play_icon smallKing' style='z-index:"+(100-arr[i])+"'  isSelect='0'></span>";
						}else if(parseInt(arr[i]) === 56){
							html+="<span  class='card card-amid play_icon bigKing' style='z-index:"+(100-arr[i])+"'  isSelect='0'></span>";
						}else{
							html+="<span  class='card-amid card play_icon "+type+"' style='z-index:"+(100-arr[i])+"' ><span class='cardFont'>"+num+"</span></span>";
						}
						}
				box.html(html);
				},
			
			
			/*创建个人信息*/
			_createInfo:function(obj,info){
				//如果该信息存在 就删除
				if($("."+obj).length!==0){
					$("."+obj).remove();
				}
				
				if(obj === "my" ){
					dataStorage.set(MyOwner.roleName,info.role);
					
					var role = dataStorage.get(MyOwner.roleName);
					
					var h = "<div  class='headIcon'>" +
								"<img src='images/xy/"+role+"/logo.png' />"+
							"</div>";
				}else if(obj === "left"){
					dataStorage.set(LeftPlayer.roleName,info.role);
					
					var role = dataStorage.get(LeftPlayer.roleName);
					var h = "<div  class='headIcon'>" +
								"<img src='images/xy/"+role+"/logo.png' />"+
							"</div>";
				}else if(obj === "right"){
					dataStorage.set(RightPlayer.roleName,info.role);
					
					var role = dataStorage.get(RightPlayer.roleName);
					var h = "<div  class='headIcon'>" +
								"<img src='images/xy/"+role+"/logo.png' />"+
							"</div>";	
				}
				
				var html = "<div class='"+obj+"'>\
								<div class='info play_icon'>\
									"+h+"\
									<div  class='pokeyName nowrap' ></div>\
									<div class='pokeyIsLandlord'></div>\
									<div  class='pokeyMy play_icon' ><span  class='pokeyDou pokeyMyIcon' ></span><span  class='pokeyMultiple'></span></div>\
									<div  class='pokeyNum' ><span class='pokeyNum-bg play_icon pokeyNum-num'></span></div>\
								</div>\
								<div class='putpokeyBox'></div>\
							</div>";
							
				$("#playBox").append(html);
				
			
				
				},
				
				
			/*自己托管*/
			_myTrusteeship:function(){
				var html = "<div class='toolTrusteeshipDiv'>"+
						"<input name='' type='button' value='取消托管'  class='pokeyButton button-blue' id='button-tg'/>"+
					"</div>";
					
				var toolTrusteeshipDiv = $(".toolTrusteeshipDiv");
				var button = $("#button-tg");
				var myPokerArr = $("#pokeyBox .card");
				myPokerArr.addClass("on");
				
				if(toolTrusteeshipDiv.length === 0){
						$("#playBox").append(html);
						toolTrusteeshipDiv = $(".toolTrusteeshipDiv");
						button = $("#button-tg");
						requestManaged();
					}else{
						toolTrusteeshipDiv.show();
						requestManaged();	
					}	
						
				_t = this;	
				button.click(function(){
					console.log("取消托管");
					_t._hideMyTrusteeship();			  
				})
				
				},
				
			_hideMyTrusteeship:function(){
				var myPokerArr = $("#pokeyBox .card");
				myPokerArr.removeClass("on");
				
				my.noPutNums=0;
				$(".toolTrusteeshipDiv").hide();
				requestUnmanaged();
				}
	}	
	
	
	
	
	
	
	
	
/*
	工具栏模块
*/

var toolBar = {
	
	/*初始化
	 * 设置背景声音this.setVideo();
	 * 声音初始化videoEffect.init();
	 * 托管初始化this.trusteeship();
	 * 快捷语初始化this.talk();
	 * 菜单初始化this.menu();*/
	init:function(){
		//this.setVideo();
		
		this.trusteeship();
		this.talk();
		this.menu();
		//this.buy();
		},
		
	/*得到三张底牌
	*/
	getBottomPokey:function(arr){
		var html="";
			for(var i=arr.length-1; i>=0; i--){
				var type = pokeyArray[arr[i]%4];
				var num = pokeyArrayNum[Math.floor(arr[i]/4)];
				
				if(parseInt(arr[i]) === 53){
					html+="<span  class='card card-small play_icon smallKing' style='z-index:"+(100-arr[i])+"' isSelect='0'></span>";
				}else if(parseInt(arr[i]) === 56){
					html+="<span  class='card card-small play_icon bigKing' style='z-index:"+(100-arr[i])+"' isSelect='0'></span>";
				}else{
					html+="<span  class='card card-small play_icon "+type+"' style='z-index:"+(100-arr[i])+"' ><span class='cardFont'>"+num+"</span></span>";
					}
				}
			
			$(".bottomPokey").html(html);
			$(".threeCard").remove();
		},
		
	/*清空顶部三张底牌*/
	clearBottomPokey:function(){
		 $(".bottomPokey").html("");
		},
		
	
	/*设置 -快捷语 */
	
	talk:function(){
		
		var html = "<div class='toolTalkDiv'>\
						<div class='toolTalkDivBg play_icon'></div>\
						<div class='toolTalkContent'>\
							<div class='toolTalkDiv-face'><h1></h1><ul class='faceLists'></ul></div>\
							<div class='toolTalkDiv-talk'><h1></h1><ul class='talkLists' >\
								<li type='0'></li>\
								<li type='1'></li>\
								<li type='2'></li>\
								<li type='3'></li>\
								<li type='4'></li>\
							</ul></div>\
						</div>\
					</div>";
		
		var toolTalk = $(".toolTalk");
		var toolTalkDiv = $(".toolTalkDiv");
		var talk =  $(".talkLists>li");
		var face =  $(".faceLists>li");
		var _t = this;
		
		 
		toolTalk.click(function(){
	    	$("#playBox").append(html);
			
			$("#playBox .faceLists").append(_t._createFace());
			
			toolTalkDiv = $(".toolTalkDiv");
			var talk =  $(".talkLists>li");
			var face =  $(".faceLists>li");
		});
	
		
		
		face.live("click",function(){
				var text = $(this).html();
				player.talk("my",text);
				gameOnSendChatMessage(text);
				toolTalkDiv.remove();
		});
		
		require(["js/mCard.js"], function(card){  
			talk.live("click",function(){
	    		var type = $(this).attr("type");
	    		var text = card.getTalkInfo(type);
	    		player.talk("my",text);
				gameOnSendChatMessage(text);
				toolTalkDiv.remove();
	    	});
		});
	
		
		
		$(document).click(function(){
				if(!$(event.target).isChildOf(".toolTalkDiv") && !$(event.target).isChildAndSelfOf(".toolTalk")){
					toolTalkDiv.remove();
					}			   
			})
		
		},
		
	
	/*设置 - 菜单*/
	menu:function(){
		var html = ""
		var toolMenu = $(".toolMenu");
		require(["js/mCommon.js","js/mBox.js"], function(common,box){  
			toolMenu.live(common.getEvent[1],function(){
				//如果是灰色的 不可点
				if($(this).hasClass("gay")){
					return; 
				}
	    		box.menu();
	    	});
		});
	},
	
	//快速购买
	buy:function(){
		var toolSet = $(".toolSet");
		require(["js/mBox.js"], function(box){  
			toolSet.click(function(){
	    		box.buy();
	    	});
		});
	},
		
		/*托管*/
		trusteeship:function(){
			var toolTrusteeship = $(".toolTrusteeship");
			
			
			 toolTrusteeship.click(function(){
				//如果是灰色的 不可点
				if($(this).hasClass("gay")){
					return; 
				}
		    	if(isGameOnGoing()){
					player.showTrusteeship("my");	
				}else{
					$().alert({"content":"请打牌时托管"});
				}
		    });
				
				
			
			},
			
			
			
		/*宝箱*/
		showChest:function(){
			if($("#chest").length === 0){
				var html = "<div id='chest' class='chest'><span class='chestBox cardBgPng'></span><div class='chestText'></div></div>";
				$("#playBox").append(html);
				this.chestBox = $("#chest");
			}else{
				this.chestBox.show();
			}
		},
		
		/*设置宝箱*/
		setChest:function(now,all,fun){
			$(".chestText",this.chestBox).html(now+"/"+all);
			this.chestBox.live("click touchend",function(){
				fun() || null;
			});
		},
		
		/*隐藏宝箱*/
		hideChest:function(){
			this.chestBox.hide();
		},
		
		/*比赛房的局数信息*/
		showGameRankInfo:function(){
			if($("#gameRandInfo").length === 0){
				var html = "<div id='gameRandInfo' class='chest'><span class='gameRandInfoBox cardBgPng'></span><div class='gameRandInfoText'></div></div>";
				$("#playBox").append(html);
				this._gameRandBox = $("#gameRandInfo");
			}else{
				this._gameRandBox.show();
			}
		},
		
		/*设置比赛房的局数信息*/
		setGameRankInfo:function(now,all){
			$(".gameRandInfoText",this._gameRandBox).html(now+"/"+all);
			this._gameRandBox.live("click touchend",function(){
				msgBox.gameRankInfo();
			});
		},
		
		/*隐藏比赛房的局数信息*/
		hideGameRankInfo:function(){
			this._gameRandBox.hide();
		},
		
		/*返回*/
		showBack:function(){
			if($("#button-back").length!==0){
				$("#button-back").remove();
			}
			var b = "<input name='' type='button' value='刷新'  class='pokeyButton pokeyButton-2 button-yellow-2 icon button-back-1' id='button-back'/>";
			$("body").append(b);
			$("#button-back").click(function(){
					window.location.reload();
					$("#button-back").remove();
				})
			},
			
		hideBack:function(){
			$("#button-back").remove();
		},
		
		/********私有方法************************************/
		_createFace:function(){
			var html = "";
			for(var i=1; i<=16; i++){
				html+="<li><img src='images/face/"+i+".gif'></li>";
				}
				
			return html;
			}
	}
	
	
	

	/*房间管理*/
	var room = {
			/*回到房间列表
			 * 删除斗地主LOAD页面$(".landlordLoad").remove();
			 * 关闭结算页面 pokeyEffect.closeResult();
			 * 隐藏MSG
			 * 创建模型this._createPlayerBox();
			 * 关闭声音videoEffect.stopAllAudio();
			 * 显示
			 **/
			goin:function(){
				pokeyEffect.closeResult();
				this._createPlayerBox();
				//Video.stopAll();
				this._dieEvent();
				//离开桌子
				gameOnStand();
				onRoomShow();
				$("#room").show();
				$("#playBox").hide();
			},
			
			// 没有未打玩的牌局，无需重联
			switchToRoom:function(){
				this._dieEvent();
				pokeyEffect.closeResult();
				$("#room").show();
				$("#playBox").hide();
				this._createPlayerBox();
				
				onRoomShow();
				
			},
			
			/*进入桌子*/
			desk:function(roomId){
				
				$("#room").hide();
				$("#playBox").show();
				$("body").attr("class","playBg");
				//toolBar.showBack();
				initGame();
				gameOnSit(roomId);
				this._setBombNums();
				player.setInfo().multiple("my",1);
				player.setInfo().multiple("left",1);
				player.setInfo().multiple("right",1);
			},
			
			/*换桌
			 * 关闭结算页面pokeyEffect.closeResult();
			 * 创建模型this._createPlayerBox();
			 * 关闭所有绑定事件this._dieEvent();
			 * 隐藏MSG
			 * 关闭显示动画豆player.clearAnimDou();*/
			changeDesk:function(){
				pokeyEffect.closeResult();
				this._createPlayerBox();
				this._dieEvent();
				player.clearAnimDou();
				initGame();
				gameOnChangeDesk();
				dataStorage.set(OtherThing.isOnChangingDesk, "true");
				this._setBombNums();
				player.setInfo().multiple("my",1);
				player.setInfo().multiple("left",1);
				player.setInfo().multiple("right",1);
			
			},
			
			/*再来一盘
			  * 关闭结算页面pokeyEffect.closeResult();
			  * 清空手牌
			  * 关闭显示动画豆player.clearAnimDou();
			  * 设置手牌张数
			  * */
			once:function(){
				
				pokeyEffect.closeResult();
				gameOnReady();	
				player.clearAnimDou();
				player.clearPut("my");
				player.clearPut("left");
				player.clearPut("right");
				player.clearMyPokey();
				player.setInfo().multiple("my",1);
				player.setInfo().multiple("left",1);
				player.setInfo().multiple("right",1);
				
				this._setBombNums();
				this._setRole();
			},
			
			/*继续比赛
			  * 关闭结算页面pokeyEffect.closeResult();
			  * 清空手牌
			  * 关闭显示动画豆player.clearAnimDou();
			  * 设置手牌张数
			  * */
			goOnGame:function(){
				
				pokeyEffect.closeResult();
				gameOnSit(getRoomId());	
				player.clearAnimDou();
				player.clearPut("my");
				player.clearPut("left");
				player.clearPut("right");
				player.clearMyPokey();
				player.setInfo().multiple("my",1);
				player.setInfo().multiple("left",1);
				player.setInfo().multiple("right",1);
				
				this._setBombNums();
			},
			/*
			 * 重置桌子状态
			 * 用于断线重联
			 */
			resetDesk:function(){
				pokeyEffect.closeResult();
				this._createPlayerBox();
				this._dieEvent();
			},
			
			/*创建房间*/
			createRoom:function(rooms){
				var G=Game;
				console.log(rooms);
				function build(rooms){
					var html = "",
					room;
					//type  <a class='png awardPng'></a>\
					for(var i=0; i<5; i++){
						room=rooms[i];
						switch(room.type){
						case 2:
							break;
						default:
							//html+="<img src='images/room_"+(i+1)+".png' offSrc='images/room_"+(i+1)+".png' onSrc='images/room_"+(i+1)+"_on.png' overSrc='images/room_"+(i+1)+"_off.png' class='room_style room_"+(i+1)+"' room='"+room.roomId+"' minBeanFun='"+room.minBeanFun+"' maxBeanFun='"+room.maxBeanFun+"' beanFunLimitDesc='"+room.beanFunLimitDesc+"' online='"+room.onlinePlayerCount+"' />";
							html+="<span class='btn_room room_pos_"+(i+1)+"' room='"+room.roomId+"' minBeanFun='"+room.minBeanFun+"' maxBeanFun='"+room.maxBeanFun+"' beanFunLimitDesc='"+room.beanFunLimitDesc+"' online='"+room.onlinePlayerCount+"'>" +
									"<span class='room_"+(i+1)+"'></span>" +
									"</span>";
						}
					}
					//html+="<img src='images/room_5.gif' offSrc='images/room_5.gif' onSrc='images/room_5_on.gif' class='room_style room_"+(i+1)+"' class='room_style room_5' room='' minBeanFun='1000000' online='' />";
					
					$("#points").html(html);
					
				}
				if(rooms){
					build(rooms);return;
				}
				G.ajax({
					url:G.getBaseURL()+"/room/showList",
					dataType:"json",
					success:function(data){
						build(data.data.rooms);
					}
				})
	
			},
			
			
			/*创建房间*/
			createGameRoom:function(rooms){
				var G=Game;
				function build(rooms){
					var matchGame = "",room;
					
					
					for(var i=0; i<rooms.length; i++){
						room=rooms[i];
					
						switch(room.type){
						case 2:
							var sta=room.isOpen?"进行中":"即将开始";
							matchGame+="<li class='item'>" +
									"<div class='game-head'>"+room.name+"</div>" +
									"<p  class='game-p'>"+room.feeEnterFee+"</p>" +
									"<div  class='game-button'><span room='"+room.roomId+"' class='pokeyButton pokeyButton-2 button-blue-2 icon' id='button-ready'><span class='text-bm num_icon'></span></span></div>" +
								"</li>";
							break;
						default:
							
						}
					}
				
					$("#gameLists").html(matchGame);
				}
				if(rooms){
					build(rooms);return;
				}
				G.ajax({
					url:G.getBaseURL()+"/room/showList",
					dataType:"json",
					success:function(data){
						build(data.data.rooms);
					}
				})
	
			},
			
			
			
			/*创建打牌场景*/
			_createPlayerBox:function(){
				var html = "<div class='toolBar'>\
					<ul>\
						<li class='toolTalk play_icon'></li>\
						<li class='toolTrusteeship gay play_icon'></li>\
						<li class='bottomPokey'></li>\
						<li class='toolSet play_icon'></li>\
						<li class='toolMenu play_icon '></li>\
					</ul>\
					<div class='baseScore'>基分：</div>\
				</div>\
				<div id='pokeyBox' onselectstart='return false;' class='pokeyBox'></div>\
				<div id='myButton' class='myButton'></div>";
	
				$("#playBox").html(html);
				require(["js/mCommon.js"], function(common){  
					common.setSize();
				});	
			},
			
			
			/*取消所有的绑定事件*/
			_dieEvent:function(){
				$("#pokeyBox>.card").die();
				$("#pokeyBox").die();
				
			},
			
			_setBombNums:function(){
				dataStorage.set(MyOwner.bombNums,0);
				dataStorage.set(RightPlayer.bombNums,0);
				dataStorage.set(LeftPlayer.bombNums,0);
				dataStorage.set(OtherThing.bombNums,0);
				$(".headIcon").removeClass().addClass("headIcon");
			},
			
			_setMultipleNums:function(){
				dataStorage.set(MyOwner.bombNums,0);
				dataStorage.set(RightPlayer.bombNums,0);
				dataStorage.set(LeftPlayer.bombNums,0);
				dataStorage.set(OtherThing.bombNums,0);
				$(".headIcon").removeClass().addClass("headIcon");
			},
			
			_setRole:function(){
				var leftRole = dataStorage.get(LeftPlayer.roleName);
				var rightRole = dataStorage.get(RightPlayer.roleName);
				var myRole = dataStorage.get(MyOwner.roleName);
				if(leftRole){
					$(".left .headIcon img").attr("src","images/xy/"+leftRole+"/logo.png");
				}
				if(rightRole){
					$(".right .headIcon img").attr("src","images/xy/"+rightRole+"/logo.png");
				}
				if(myRole){
					$(".my .headIcon img").attr("src","images/xy/"+myRole+"/logo.png");
				}
			}
			
	}


	/*公共的一些效果 三个都有
	 炸弹 bomb
	 飞机 plain
	 火箭 rocket

	 地主赢了 landlordWin
	 地主输了 landlordLose
	 农民赢了 farmerWin
	 农民输了 farmerLose
	*/

		
	var pokeyEffect = {
			
			anmi:function(arr,obj){
				var type = Rule.getSType(arr);
				console.log("比赛类型："+obj)
				/*判断炸弹*/
				if(type === 1009 || type === 1008){
					require(["js/mEffect.js"], function(effect){    
						//effect.addBombNums(obj);
					}); 
					
					console.log(obj+"出的炸弹！");
				}
				
				/*判断飞机*/
				if(type === 1016 || type === 1017 || type === 1018){
					this.plain();
				}
			},
			
			
				
			plain:function(){
				this._createPlain();
				Video.plain();
				},
				
			
			
			landlordWin:function(obj){
				player.showAnimDou(obj);
				require(["js/mEffect.js"], function(effect){  
					effect.result("win",obj);
				});
				
				
				},
				
			landlordLose:function(obj){
				player.showAnimDou(obj);
				require(["js/mEffect.js"], function(effect){  
					effect.result("lose",obj);
				});	
				},
				
			farmerWin:function(obj){
				player.showAnimDou(obj);
				require(["js/mEffect.js"], function(effect){  
					effect.result("win",obj);
				});	
				},
				
			farmerLose:function(obj){
				player.showAnimDou(obj);
				require(["js/mEffect.js"], function(effect){  
					effect.result("lose",obj);
				});	
				},
			
			bossWin:function(obj){
				
				require(["js/mEffect.js"], function(effect){  
					effect.bossResult("win",obj);
				});	
				},
					
			bossLose:function(obj){
				
				require(["js/mEffect.js"], function(effect){  
					effect.bossResult("lose",obj);
				});	
				},
			
				
			/*关闭结果页面*/
			closeResult:function(){
				 $("#result").remove();
			},
			
			
				
			/*
			构建一个飞机
			*/
			_createPlain:function(){
				this.plainBox = $("#plain");
				
				if(this.plainBox.length === 0){
					var html = "<div id='plain' class='effectPng'></div>";
					$("body").append(html);
					this.plainBox = $("#plain");
					}
				
				this.plainBox.css({"left":"100%"});
				this.plainBox.animate({"left":"-10%"},2000);
				
				}
				
		
				
			
		}
		
		
	
